﻿Public Class TMyServerInfoListItem
    Friend _DisplayItem As String
    Friend _SQL As String
    Public ReadOnly Property DisplayItem() As String
        Get
            Return _DisplayItem
        End Get
    End Property
    Public ReadOnly Property SQL() As String
        Get
            Return _SQL
        End Get
    End Property
End Class

Public Class TMyServerInfo
    Private _MyQuery As New TMyQuery
    Private _SQLList As New List(Of TMyServerInfoListItem)
    Private _SelectedIndex As Integer
    Private _Condition As String = String.Empty
    Public Sub New(ByVal AConnection As TMySession)
        _MyQuery.Session = AConnection
        Init()
    End Sub
    Public Property Condition() As String
        Get
            Return _Condition
        End Get
        Set(ByVal value As String)
            _Condition = value
        End Set
    End Property
    Public ReadOnly Property DisplayItemCount() As Integer
        Get
            Return _SQLList.Count
        End Get
    End Property
    Public Property SelectedIndex() As Integer
        Get
            Return _SelectedIndex
        End Get
        Set(ByVal value As Integer)
            _SelectedIndex = value
        End Set
    End Property

    Public Sub Refresh()
        _MyQuery.Close()
        _MyQuery.Clear()
        _MyQuery.SQL.AppendLine(_SQLList(_SelectedIndex).SQL)
        If _Condition.Length > 0 Then
            _MyQuery.SQL.AppendLine("like ?condition")
            _MyQuery.DeclareAndSet("condition", "%" & _Condition & "%")
        End If
        _MyQuery.Execute()
    End Sub

    Public ReadOnly Property DataTable() As DataTable
        Get
            Return _MyQuery.DataSet.Tables(0)
        End Get
    End Property
    Public ReadOnly Property SQLItem(ByVal Index As Integer) As String
        Get
            Return _SQLList(Index).SQL
        End Get
    End Property

    Public ReadOnly Property DisplayItem(ByVal Index As Integer) As String
        Get
            Return _SQLList(Index).DisplayItem
        End Get
    End Property

    Private Sub Init()
        _SelectedIndex = 0
        _SQLList.Clear()
        Dim item As TMyServerInfoListItem
        item = New TMyServerInfoListItem
        item._DisplayItem = "SESSION STATUS"
        item._SQL = "SHOW SESSION STATUS"
        _SQLList.Add(item)

        item = New TMyServerInfoListItem
        item._DisplayItem = "GLOBAL STATUS"
        item._SQL = "SHOW GLOBAL STATUS"
        _SQLList.Add(item)

        item = New TMyServerInfoListItem
        item._DisplayItem = "SESSION VARIABLES"
        item._SQL = "SHOW SESSION VARIABLES"
        _SQLList.Add(item)

        item = New TMyServerInfoListItem
        item._DisplayItem = "GLOBAL VARIABLES"
        item._SQL = "SHOW GLOBAL VARIABLES"
        _SQLList.Add(item)

    End Sub
End Class
